<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/7/6
 * Time: 15:44
 */

namespace app\index\controller;

use FFMpeg\Coordinate\Dimension;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\FFMpeg;
use think\Controller;
use think\Db;
use think\Request;

class Upload extends Controller
{
    public function index()
    {
        return $this->fetch('upload');
    }

    /**
     *文件上传
     */
    public function upload()
    {
        $request = Request::instance();
        $img_host = $request->domain();

        $file = request()->file('video');
        if($file)
        {
            //move()方法会将上传的文件默认的移到public下,并生成ss目录,且还会在ss目录下生成一个db目录
            //$info = $file->rule('md5')->move('ss');

            $info = $file->move(ROOT_PATH.'public'.DS.'uploads');   // DS就是一个分割符  /

            if($info)
            {
                //echo $info->getExtension(),'<br>';     //获取文件的扩展名
                //echo $info->getSaveName(),'<br>';     //获取上传文件保存的第一级目录名和文件名
                //echo $info->getFilename(),'<br>';     //获取文件上传后的文件名

                $img_src = $info->getSaveName();
                $img_src = str_replace('\\','/',$img_src);     // 20180706/sdasfasfasgasgasga.xxx
                $img_url = $img_host.'/uploads/'.$img_src;       //这个是完整的图片访问url
                //$filePath = $img_src;
                $filePath = $img_url;
                $data = ['video_url'=>$filePath];
                $res = Db::table('video')->insert($data);
                if($res)
                {
                    $this->success('文件上传成功', 'index/upload/index');
                }else{
                    $this->error('文件上传失败', 'index/upload/index');
                }
            }else{
                //上传失败获取错误信息
                echo $file->getError();
            }
        }
    }

    public function lujin()
    {
        $request = Request::instance();
        echo $request->domain();    //返回http://tp5.cc:8080
    }
    /*
     * 读取自定义配置项
     */
    public function visit_Img()
    {
        $img_host = config('visit_img.img_prefix');
        $img_url = Db::table('video')->where('id',4)->column('video_url');
        return $img_host.$img_url[0];
    }

    /**
     * 截取视频gif
     */
    public function gif()
    {
        $ffmpeg = FFMpeg::create(array('ffmpeg.binaries'=>'E:\laragon\bin\php\php-7.2.2-Win32-VC15-x64\ffmpeg\bin\ffmpeg.exe',
                                       'ffprobe.binaries'=>'E:\laragon\bin\php\php-7.2.2-Win32-VC15-x64\ffmpeg\bin\ffprobe.exe' ));

        $video_url = Db::table('video')->where('id',5)->find();
        $video = $ffmpeg->open('uploads/20180710/67566c149118e21705a381e80e92db74.mp4');
        $video->frame(TimeCode::fromSeconds(5))->save('uploads/frame.jpg');
        //$frame->save('uploads/image.jpg');
                                                                                         //gif动图显示时间
        $video->gif(TimeCode::fromSeconds(5),new Dimension(300,400),2)->save('uploads/frame1.gif');
    }


}